package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.fragment.SocietyListFragment;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ToastUtil;

public class SearchSocietyActivity extends BaseToolBarActivity implements View.OnClickListener {
    //搜索输入框
    private EditText metSearch;
    //搜索按钮
    private ImageView mIvSearch;

    private SocietyListFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_society);
        initWidget();
        initListener();
        attachFragment();
    }

    @Override
    public void initWidget() {
        metSearch = (EditText) findViewById(R.id.et_search);
        mIvSearch = (ImageView) findViewById(R.id.iv_search);
        mIvSearch.setOnClickListener(this);
    }

    /**
     *
     */
    public void attachFragment() {
        fragment = SocietyListFragment.createSearchListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, fragment).commit();
    }


    @Override
    public void initListener() {
        metSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                searchSociety();
                return false;
            }
        });

    }

    /**
     *清除编辑框
     */
    public void clearEdit() {
        metSearch.setText("");
        fragment.research(metSearch.getText().toString());
    }

    /**
     * 搜索社团
     */
    public void searchSociety() {
        if (metSearch.getText().length() == 0) {
            ToastUtil.showMessage("请输入查询条件!");
            InputMethodUtil.reuestFocus(metSearch);
            return;
        }
        fragment.research(metSearch.getText().toString());

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search:
                clearEdit();
                break;
            default:
                break;

        }
    }
}
